/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2016 - 2021 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.features.module.modules.exploit

import net.ccbluex.liquidbounce.config.NamedChoice
import net.ccbluex.liquidbounce.features.module.Category
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.utils.client.asText
import net.ccbluex.liquidbounce.utils.client.chat
import net.minecraft.network.packet.c2s.play.PlayerInteractEntityC2SPacket
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket
import net.minecraft.text.TranslatableText
import net.minecraft.util.Formatting
import kotlin.random.Random

/**
 * Kick module
 *
 * Kick yourself from the server.
 */
object ModuleKick : Module("Kick", Category.EXPLOIT, disableActivation = true) {

    private val mode by enumChoice("Mode", KickModeEnum.QUIT, KickModeEnum.values())

    override fun enable() {
        if (mc.isIntegratedServerRunning) {
            chat(
                "${TranslatableText("liquidbounce.generic.error")}: ".asText()
                    .styled { it.withColor(Formatting.RED).withBold(true) },
                this.message(
                    "cantEnableInSingleplayer",
                    "'Kick'".asText().styled { it.withColor(Formatting.RED).withBold(true) }
                ).styled { it.withColor(Formatting.GREEN) }
            )
            return
        }

        kick(mode)
    }

    /**
     * Kick client player
     *
     * @warning Only use on multiplayer! This function can cause the game to crash when used on a integrated server.
     */
    fun kick(mode: KickModeEnum) = mode.run()

    enum class KickModeEnum(override val choiceName: String, val run: () -> Unit) : NamedChoice {
        QUIT(
            "Quit",
            {
                world.disconnect()
            }
        ),
        INVALID_PACKET(
            "InvalidPacket",
            {
                network.sendPacket(
                    PlayerMoveC2SPacket.PositionAndOnGround(
                        Double.NaN,
                        Double.NEGATIVE_INFINITY,
                        Double.POSITIVE_INFINITY,
                        !player.isOnGround
                    )
                )
            }
        ),
        SELF_HURT(
            "SelfHurt",
            {
                network.sendPacket(PlayerInteractEntityC2SPacket.attack(player, player.isSneaking))
            }
        ),
        ILLEGAL_CHAT(
            "IllegalChat",
            {
                player.sendChatMessage("§${Random.nextInt()}§${Random.nextInt()}§")
            }
        )
    }

}
